﻿using Assets.Scripts.DL.Enemy.BaseClass.StateData;
using Assets.Scripts.DL.Mgrs;
using UnityEngine;

namespace Assets.Scripts.DL.Enemy.CommonEnemy.States {
	/// <summary>
	/// 普攻
	/// </summary>
	public class Atk : CommonState<Controller> {
		Timer _timer;
		bool _sendDamage;
		//bool _animStart;

		Vector2 _atkOffset;
		float _range;
		int _damage;

		public Atk(Controller controller, StateEnum stateName) : base(controller, stateName) {
			_timer = _controller.StateData.AtkCdRecoder[StateName];

			AddConditionToSelf(() => _controller.IsPlayerInAtkRange()
			&& _timer.Elapsed > _controller.Config.CommonAtkGap);
		}

		public override void Enter() {

			_controller.PlayAnim(StateName);
			_controller.Velocity = Vector2.zero;
			_controller.StateData.AtkOver = false;
			//_animStart = false;
			_sendDamage = false;

			_atkOffset = _controller.Config.AtkOff;
			_range = _controller.Config.AtkDis;
			_damage = _controller.Config.Damage;
		}

		public override void Update() {
			if (_controller.IsAnimPlayAt(0.6f) && !_sendDamage) {
				CheckDamage(ref _sendDamage);
			}
			if (_controller.IsAnimPlayOver()) {
				_controller.StateData.AtkOver = true;
			}
		}

		public override void Exit() {
			_timer.Reset();
			_controller.StateData.LastAtkTime.Reset();
		}
		void CheckDamage(ref bool dmgSend) {
			if (_controller.IsAnimPlayAt(0.65f)) {
				_controller.CheckAtk(_atkOffset, _range, _damage);
				dmgSend = true;
			}
		}
	}
}
